package netsphere.moonend.test;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

// Download jar file to use google translate
// Site:		http://code.google.com/p/google-api-translate-java/
// Download:	google-api-translate-java-0.95.jar file

import com.google.api.translate.Translate;
import com.google.api.translate.Language;

public class GoogleTranslationAPI {

	protected Shell shell;
	private Text origianlTextBox;
	private Text translatedTextBox;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			GoogleTranslationAPI window = new GoogleTranslationAPI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(581, 241);
		shell.setText("SWT Application");
		
		Label lblOriginalTextLabel = new Label(shell, SWT.NONE);
		lblOriginalTextLabel.setBounds(10, 13, 87, 15);
		lblOriginalTextLabel.setText("OriginalText:");
		
		Label lblTranslatedTextLabel = new Label(shell, SWT.NONE);
		lblTranslatedTextLabel.setBounds(10, 108, 87, 15);
		lblTranslatedTextLabel.setText("TranslatedText:");
		
		origianlTextBox = new Text(shell, SWT.BORDER | SWT.MULTI);
		origianlTextBox.setBounds(110, 10, 314, 89);
		
		translatedTextBox = new Text(shell, SWT.BORDER | SWT.MULTI);
		translatedTextBox.setEnabled(false);
		translatedTextBox.setBounds(110, 105, 314, 89);
		
		// You can use any string values in parameter of Translate.setHttpReferrer()
		Translate.setHttpReferrer("http://netsphere.tistory.com");
		
		// Define Button and related functions 
		Button btnTranslateButton = new Button(shell, SWT.NONE);
		btnTranslateButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String from = origianlTextBox.getText().toString();
				String after = new String();
				
				try	{
					after = Translate.execute(from, Language.AUTO_DETECT, Language.KOREAN);
				}	catch (Exception e1)	{
					e1.printStackTrace();
				}
				translatedTextBox.setText(after);
			}
		});
		btnTranslateButton.setBounds(430, 8, 125, 43);
		btnTranslateButton.setText("Translate to Korean");

	}
}
